package com.lp.batch;

import org.exolab.castor.xml.Unmarshaller;
import org.springframework.oxm.castor.CastorMarshaller;

import com.lp.batch.domain.Node;

/**
 * A custom CastorMarshaller that has a UnmarshallerListener which register a parent node to every childnodes
 * 
 * @author Zainul Franciscus
 * 
 */
public class TaxonomyUnmarshaller extends CastorMarshaller {

	/**
	 * This method register an UnmarshallerListener.
	 * 
	 * @see org.springframework.oxm.castor.CastorMarshaller#customizeUnmarshaller(org.exolab.castor.xml.Unmarshaller)
	 */
	@Override
	protected void customizeUnmarshaller(Unmarshaller unmarshaller) {
		unmarshaller.setUnmarshalListener(new org.castor.xml.UnmarshalListener() {
			
			/*
			 * When Castor finishes unmarshalling an object, this method
			 * set a parent node to a child node.
			 * 
			 * @see
			 * org.castor.xml.UnmarshalListener#unmarshalled(java.lang.Object,
			 * java.lang.Object)
			 */
			public void unmarshalled(Object target, Object parent) {

				if (target instanceof Node && parent instanceof Node) {
					Node thisNode = (Node) target;
					Node parentNode = (Node) parent;
					
					thisNode.setParentNode(parentNode);
				}

			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see
			 * org.castor.xml.UnmarshalListener#initialized(java.lang.Object,
			 * java.lang.Object)
			 */
			public void initialized(Object target, Object parent) {
				

			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see
			 * org.castor.xml.UnmarshalListener#attributesProcessed(java.lang
			 * .Object, java.lang.Object)
			 */
			public void attributesProcessed(Object target, Object parent) {
				

			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see
			 * org.castor.xml.UnmarshalListener#fieldAdded(java.lang.String,
			 * java.lang.Object, java.lang.Object)
			 */
			public void fieldAdded(String fieldName, Object parent, Object child) {
				

			}

			

		});
		super.customizeUnmarshaller(unmarshaller);
	}

}
